package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import io.renren.common.utils.ShiroUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.WebContentTypeEntity;
import io.renren.modules.generator.service.WebContentTypeService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 首页内容类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-11-20 11:07:46
 */
@RestController
@RequestMapping("generator/webcontenttype")
@Api(value = "首页内容类型",tags = {"首页内容类型"})
public class WebContentTypeController {
    @Autowired
    private WebContentTypeService webContentTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("generator:webcontenttype:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = webContentTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{contentTypeId}")
    @RequiresPermissions("generator:webcontenttype:info")
    public R info(@PathVariable("contentTypeId") Integer contentTypeId){
		WebContentTypeEntity webContentType = webContentTypeService.getById(contentTypeId);

        return R.ok().put("webContentType", webContentType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:webcontenttype:save")
    public R save(@RequestBody WebContentTypeEntity webContentType){
        SysUserEntity userEntity = ShiroUtils.getUserEntity();
        Long userId = userEntity.getUserId();
        webContentType.setCreateUser(Math.toIntExact(userId));
        webContentType.setCreateTime(new Date());
        webContentType.setUpdateTime(new Date());
		    webContentTypeService.save(webContentType);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:webcontenttype:update")
    public R update(@RequestBody WebContentTypeEntity webContentType){
		webContentTypeService.updateById(webContentType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:webcontenttype:delete")
    public R delete(@RequestBody Integer[] contentTypeIds){
		webContentTypeService.removeByIds(Arrays.asList(contentTypeIds));

        return R.ok();
    }

}
